
#include <utest.h>
#include <hello/conc/conc_queue.h>


template<typename QueueT>
void queue_benchmark(size_t conc, size_t sc)
{
    QueueT que(conc * sc);
    std::vector<std::thread> ts;
    ts.reserve(conc);
    auto st = std::chrono::high_resolution_clock::now();
    for (auto i = 0; i < conc; ++i)
    {
        ts.emplace_back([&que, &sc]() {
            for (auto j = 0; j < sc; ++j)
                que.push("dsdsadsadassdsdsdsdsdsdsdsdasdsadsdsadsadassdsdsdsdsdsdsdsdasdsadsdsadsadassdsdsdsdsdsdsdsdasdsa");
        });
    }
    size_t tot_c = conc * sc;
    std::thread consumer([&que, &tot_c]() {
        for (auto j = 0; j < tot_c; ++j)
            auto ans = que.poll();
    });
    for (auto& t : ts) if (t.joinable()) t.join();
    if (consumer.joinable()) consumer.join();
    auto ed = std::chrono::high_resolution_clock::now();
    println("cost:", std::chrono::duration_cast<std::chrono::milliseconds>(ed - st).count(), "ms");
    println("final size:", que.size());
}


TEST(benchmark, queue, true)
{
    size_t maxc = 600000;
    size_t concs[4] = { 2, 4, 6, 8 };
    for (auto i = 0; i < 4; ++i)
    {
        println("---------------", concs[i], "------------------------------");
        println("===[[SC QUEUE]]===");    queue_benchmark<hello::scqueue<std::string>>(concs[i], maxc / concs[i]);
        println("===[[SU QUEUE]]===");    queue_benchmark<hello::suqueue<std::string>>(concs[i], maxc / concs[i]);
        println("===[[HC QUEUE]]===");    queue_benchmark<hello::hcqueue<std::string>>(concs[i], maxc / concs[i]);
        println("===[[HU QUEUE]]===");    queue_benchmark<hello::huqueue<std::string>>(concs[i], maxc / concs[i]);
    }
}

